#include "LogUtils.h"
#include <iostream>
#include <log4cpp/DailyRollingFileAppender.hh> 
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Appender.hh> 

using std::cout;
using std::endl;

LogUtils& LogUtils::getInstance() {
    static LogUtils instance;
    return instance;
}

LogUtils::LogUtils() : m_root(log4cpp::Category::getRoot()) {
    // 控制台输出
    log4cpp::PatternLayout* pLayoutConsole = new log4cpp::PatternLayout();
    pLayoutConsole->setConversionPattern("%d{%Y-%m-%d %H:%M:%S.%l} %p %c: %m%n");

    log4cpp::OstreamAppender* pOstreamAppender = 
        new log4cpp::OstreamAppender("console", &std::cout);
    pOstreamAppender->setLayout(pLayoutConsole);

    // 文件输出 (使用最兼容的构造函数)
    
    log4cpp::PatternLayout* pLayoutFile = new log4cpp::PatternLayout();
    pLayoutFile->setConversionPattern("%d{%Y-%m-%d %H:%M:%S.%l} [%5p] %c - %m%n"); 

    log4cpp::DailyRollingFileAppender* pDailyRollingFileAppender = 
        new log4cpp::DailyRollingFileAppender("dailyRollingFile", 
                                             "logs/app.log",      // 文件名
                                             5);             // 默认保留 5 天的备份

    pDailyRollingFileAppender->setLayout(pLayoutFile); 

    // 配置 Root Logger ---
    
    m_root.setPriority(log4cpp::Priority::INFO); 

    m_root.addAppender(pOstreamAppender);
    m_root.addAppender(pDailyRollingFileAppender);
}

log4cpp::Category& LogUtils::getRootLogger() {
    return m_root;
}